function decomposePath(aPath) {
  let directoryPos = {}, directoryLen = {},
      basenamePos  = {}, basenameLen  = {},
      extensionPos = {}, extensionLen = {};
  URLParser.parseFilePath(aPath, aPath.length,
                          directoryPos, directoryLen,
                           basenamePos,  basenameLen,
                          extensionPos, extensionLen);
  let dir = aPath.substr(directoryPos.value, directoryLen.value);
  let base = aPath.substr(basenamePos.value, basenameLen.value);
  let ext = aPath.substr(extensionPos.value, extensionLen.value);

  return [dir ? ((dir == "/") ? dir : dir.substr(0, dir.length - 1)) : ".",
          base ? ((aPath[aPath.length - 1] == ".") ? base.substr(0, base.length - 1) : base) : false,
          ext ? ext : (aPath[aPath.length - 1] == ".") ? "" : false];
}

function _parseFileName(aPath) {
  let basenamePos  = {}, basenameLen  = {},
      extensionPos = {}, extensionLen = {};
  URLParser.parseFileName(aPath, aPath.length,
                          basenamePos, basenameLen,
                          extensionPos, extensionLen);
  return [aPath.substr(basenamePos.value, basenameLen.value),
          aPath.substr(extensionPos.value, extensionLen.value)];
}

function pathExtesion(aPath) {
  return _parseFileName(aPath)[1];
}

function pathSansExtension(aPath) {
  return _parseFileName(aPath)[0];
}

function sysBasename(aPath) {
  if (!aPath) return "";
  if (aPath == "/") return "";
  if (aPath[aPath.length - 1] == "/") aPath = aPath.substr(0,aPath.length - 1);
  let [dir, base, ext] = decomposePath(aPath);
  return ext ? base + "." + ext : base;
}

function sysDirname(aPath) {
  if (!aPath) return ".";
  if (aPath == "/") return "/";
  if (aPath[aPath.length - 1] == "/") aPath = aPath.substr(0,aPath.length - 1);
  let [dir, base, ext] = decomposePath(aPath);
  return dir;
}

function convertURIToFilePath(aURI) {
  let uri = IOService.newURI(aURI, null, null);
  if (!uri.schemeIs('file')) return false;
  let fileHandler = IOService.getProtocolHandler('file')
                      .QueryInterface(Ci.nsIFileProtocolHandler);
  return fileHandler.getFileFromURLSpec(aURI).path;
}

function makeURIFromSpec(aURI) {
  if (!aURI) return false;
  if (/^file:/.test(aURI)) {
    let fileHandler = IOService.getProtocolHandler('file')
                        .QueryInterface(Ci.nsIFileProtocolHandler);
    return IOService.newFileURI(fileHandler.getFileFromURLSpec(aURI)).QueryInterface(Ci.nsIURL);
  }
  return IOService.newURI(aURI, null, null).QueryInterface(Ci.nsIURL);
}